package com.thewell79.areadiary.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.cengalabs.flatui.views.FlatEditText;
import com.dd.processbutton.iml.ActionProcessButton;
import com.thewell79.areadiary.BaseApplication;
import com.thewell79.areadiary.R;
import com.thewell79.areadiary.activity.MainActivity;
import com.thewell79.areadiary.data.UserData;
import com.thewell79.areadiary.request.HttpConfig;
import com.thewell79.areadiary.request.PostStringRequest;

import java.util.HashMap;

/**
 * Created by THEWELL-LOBBY on 2015-04-27.
 */

public class SignUpFragment extends Fragment {

    private FlatEditText email, password, re_password;
    private ActionProcessButton btn_signup;

    private PostStringRequest request = new PostStringRequest(Request.Method.PUT, HttpConfig.USER);

    private PostStringRequest.OnReqeust onReqeust = new PostStringRequest.OnReqeust() {

        @Override
        public void onLoading() {
            btn_signup.setProgress(1);
        }

        // 정상적으로 값을 가져왔다면
        @Override
        public void onEndRequest(String response) {

        }

        @Override
        public void onEnd(boolean isError, VolleyError error) {
            if (isError) {
                btn_signup.setProgress(100);
            } else {
                btn_signup.setProgress(-1);
            }
        }
    };

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String id = email.getText().toString();
            String pw = password.getText().toString();
            String re_pw = re_password.getText().toString();

            if (Patterns.EMAIL_ADDRESS.matcher(id).matches()) {
                // 비밀번호가 일치하면
                if (pw.equals(re_pw)) {
                    HashMap<String, String> params = new HashMap<>();
                    params.put("UserId", id);
                    params.put("UserPw", pw);

                    request.setParams(params);

                    request.commit();
                    // 일치하지 않으면
                } else {
                    Toast.makeText(getActivity(), "비밀번호를 다시 확인해 주세요", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(getActivity(), "정확한 이메일을 입력해 주세요", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_signup, container, false);

        email = (FlatEditText) v.findViewById(R.id.edit_email);
        password = (FlatEditText) v.findViewById(R.id.edit_password);
        re_password = (FlatEditText) v.findViewById(R.id.edit_repassword);

        btn_signup = (ActionProcessButton) v.findViewById(R.id.btn_signup);

        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        request.setOnLoading(onReqeust);

        btn_signup.setMode(ActionProcessButton.Mode.ENDLESS);
        btn_signup.setOnClickListener(onClickListener);
    }
}
